package cn.edu.xcu.ckl.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.edu.xcu.ckl.LoginActivity;
import cn.edu.xcu.ckl.OrderListActivity;
import cn.edu.xcu.ckl.R;
import cn.edu.xcu.ckl.base.BaseFragment;
import cn.edu.xcu.ckl.entity.UserInfo;
import cn.edu.xcu.ckl.utils.GlideEngine;


public class MineFragment extends BaseFragment {
    private TextView username;
    private ImageView avatar;

    @Override
    protected int getContentView() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        username = rootView.findViewById(R.id.username);
        avatar = rootView.findViewById(R.id.avatar);
    }

    @Override
    protected void setListener() {

        //订单
        rootView.findViewById(R.id.lin_order).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserInfo userInfo = UserInfo.getUserInfo();
                if (userInfo != null) {
                    startActivity(new Intent(getActivity(), OrderListActivity.class));
                } else {
                    showToast("请先登录");
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }

            }
        });

        //退出登录
        rootView.findViewById(R.id.exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(getActivity()).setTitle("退出登录提醒").setMessage("确定要退出登录吗？").setNegativeButton("取消", null).setPositiveButton("退出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        UserInfo.setUserInfo(null);
                        if (getActivity() != null) {
                            getActivity().finish();
                            startActivity(new Intent(mContext, LoginActivity.class));
                        }
                    }
                }).show();
            }
        });

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        UserInfo userInfo = UserInfo.getUserInfo();
        if (null != userInfo) {
            username.setText(userInfo.getUsername());
            if (!TextUtils.isEmpty(userInfo.getAvatar())) {
                GlideEngine.createGlideEngine().loadImage(getContext(), userInfo.getAvatar(), avatar);
            }
            username.setOnClickListener(null);
        } else {
            username.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            });
        }
    }
}